<?php


namespace app\api\model;


use app\common\library\AMapModel;
use app\common\library\IDMan;
use app\common\model\BaseModel;
use ApiOper;
use common;
use think\Db;
use think\db\Query;
use think\Exception;
use think\facade\Log;

class BikeOperRecordModel extends BaseModel
{
    protected $name = 'bike_oper_record';
    protected $pk = 'id';

    function add($type, $city_id, $bike_id, $bound_order_info, $result, $channel = null)
    {
        $bike_info = BikeModel::instance()->get_info_by_id($bike_id);

        $info = [];
        $info['city_id'] = $city_id;
        $info['bike_sn'] = $bike_info['sn'];
        $info['bike_id'] = $bike_id;
        if ($bound_order_info) {
            $info['bound_order_type'] = $bound_order_info['order_type'];
            $info['bound_order_id'] = $bound_order_info['order_id'];
            $info['bound_order_op'] = $bound_order_info['op'];
        }

        $info['health_state'] = $bike_info['health_state'];
        $info['is_online'] = $bike_info['is_online'];
        $info['speed'] = $bike_info['speed'];
        $info['is_in_motion'] = $bike_info['is_in_motion'];
        $info['is_on_battery'] = $bike_info['is_on_battery'];
        $info['battery_level'] = $bike_info['battery_level'];
        $info['coordinate'] = $bike_info['coordinate'];
        $info['address'] = $bike_info['address'];

        $info['type'] = $type;

        $userinfo = ApiOper::instance()->userinfo();
        $info['id'] = IDMan::city_order_id($info['city_id']);
        $info['create_time'] = date('Y-m-d H:i:s');
        $info['user_id'] = $userinfo['id'];
        $info['user_name'] = $userinfo['name'];
        $info['user_phone'] = $userinfo['phone'];
        $info['user_type'] = $userinfo['user_type'];
        if (empty($channel)) {
            $info['channel'] = $userinfo['channel'];
        } else {
            $info['channel'] = $channel;
        }

        if ($result) {
            $info['success'] = $result[0];
            $info['error_msg'] = $result[1];
            $info['cost'] = $result[2];
        }

        if (isset($userinfo['user_coordinate'])) {
            $info['user_address'] = (new AMapModel())->map_c_2_a($userinfo['user_coordinate']);
            $info['user_coordinate'] = $userinfo['user_coordinate'];
        }
        self::create($info);
    }

    function query_list($city_id, $cd, $pos, $limit)
    {
        $limit = min(30, $limit);
        $selectlist = [
            'id' => 'string',
            'bike_id' => 'string',
            'bike_sn' => 'string',
            'channel' => 'string',
            'type' => 'int',
            'health_state' => 'int',
            'is_online' => 'int',
            'speed' => 'int',
            'is_in_motion' => 'int',
            'is_on_battery' => 'int',
            'battery_level' => 'int',
            'coordinate' => 'array',
            'address' => 'string',
            'user_phone' => 'string',
            'user_name' => 'string',
            'user_id' => 'string',
            'create_time' => 'string',
            'bound_order_type' => 'string',
            'bound_order_id' => 'string',
            'bound_order_op' => 'string',
            'user_type' => 'int',
            'cost' => 'int',
            'success' => 'int',
            'error_msg' => 'string',
            'user_coordinate' => 'array',
            'user_address' => 'string',
        ];

        $sfs = common::array_keys($selectlist);
        $beans = [];
        if (!empty($city_id)) {
            $beans['city_id'] = $city_id;
        }

        if (isset($cd['bike_id'])) {
            $beans['bike_id'] = $cd['bike_id'];
        }

        if (isset($cd['user_id'])) {
            $beans['user_id'] = $cd['user_id'];
        }

        if (isset($cd['user_type'])) {
            $beans['user_type'] = $cd['user_type'];
        }

        $query = new Query();

        if (isset($cd['type'])) {
            $query->where("type >= :v and type < :v1+10",['v'=>$cd['type'],'v1'=>$cd['type']]);
        }

        if (isset($cd['bound_order_id'])) {
            $beans['bound_order_id'] = $cd['bound_order_id'];
        }

        $rows = $this->where($beans)->where($query)->field($sfs)->order('create_time','desc')->limit($pos,$limit)->select();
        $l = [];
        foreach ($rows as $row) {
            $l[] = common::sql_row($selectlist, $row);
        }
        return $l;
    }

    function archive($days, $limit)
    {
        $selectlist = [
            'id' => 'string',
            'bike_id' => 'string',
            'bike_sn' => 'string',
            'channel' => 'string',
            'type' => 'int',
            'health_state' => 'int',
            'is_online' => 'int',
            'speed' => 'int',
            'is_in_motion' => 'int',
            'is_on_battery' => 'int',
            'battery_level' => 'int',
            'coordinate' => 'array',
            'address' => 'string',
            'user_phone' => 'string',
            'user_name' => 'string',
            'user_id' => 'string',
            'create_time' => 'string',
            'bound_order_type' => 'string',
            'bound_order_id' => 'string',
            'bound_order_op' => 'string',
            'user_type' => 'int',
            'cost' => 'int',
            'success' => 'int',
            'error_msg' => 'string',
            'user_coordinate' => 'array',
            'user_address' => 'string',
        ];

        $datetime = date('Y-m-d', strtotime("-{$days} days"));
        $sfs =common::array_keys($selectlist);
        $rows = Db::table('tb_bike_oper_record')->where('create_time','<',$datetime)->field($sfs)->limit($limit)->select();
        var_dump($rows);

        $l = [];
        foreach ($rows as $row) {
            $info = common::sql_row($selectlist, $row);
            $season = date('Ym', strtotime($info['create_time']));
            try {
//                Db::table('tb_bike_oper_record')->insert("tb_bike_oper_record_" . $season, $info);
//                hisql::db()->execute("delete from tb_bike_oper_record where id = ?", [$info['id']]);
                Db::table('tb_bike_oper_record')->where('id',$info['id'])->delete();
            } catch (Exception $e) {
                Log::error("archive bike oper error".$e->getMessage());
            }

        }
    }
}